package com.itboye.ihomebank.me;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.ChengShiAdapter;
import com.itboye.ihomebank.adapter.QuAdapter;
import com.itboye.ihomebank.adapter.QuBean;
import com.itboye.ihomebank.adapter.QuYuAdapter;
import com.itboye.ihomebank.adapter.XiaoQuYuAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.AvatarBeans;
import com.itboye.ihomebank.bean.AvatarDataBeans;
import com.itboye.ihomebank.bean.EditBean;
import com.itboye.ihomebank.bean.QuYuBean;
import com.itboye.ihomebank.bean.QuYuBean.Children;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XGridView;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.CityChildrenWheelAdapter;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.CityWheelAdapter;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.XiaoQuChildWheelAdapter;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.XiaoQuFatherWheelAdapter;
import com.itboye.ihomebank.me.ActivityFaBuHouse.GridAdapter;
import com.itboye.ihomebank.photo.Bimp;
import com.itboye.ihomebank.photo.CopyOfPhotoActivity;
import com.itboye.ihomebank.photo.FileUtils;
import com.itboye.ihomebank.photo.TestPicActivity;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.uploadmultipleimage.MultipartRequest;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.ChengShiPopupWindow;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.UploadImage.OnUploadlistener;
import com.itboye.ihomebank.util.XImageLoader;
import com.itboye.ihomebank.util.XiaoQuPopupWindow;
import com.itboye.ihomebank.wheelview.OnWheelScrollListener;
import com.itboye.ihomebank.wheelview.WheelView;

/**
 * 
 * 发布房源（1）
 * 
 */
public class ActivityEditHouse extends BaseOtherActivity implements Observer,
		OnUploadlistener {
	ImageView fabu_house_close;
	private ImageView fabu_house_img;// 大图
	private LinearLayout fabu_house_imgs;
	private ImageView fabu_house_add;// 添加图片
	private TextView fabu_house_ok;// 下一步
	private TextView fabu_house_fangshi;
	private EditText fabu_house_title, fabu_house_phone, fabu_house_people;

	private XGridView noScrollgridview;
	private GridAdapter adapter;

	RelativeLayout re_choose_city;// 选择市区
	TextView tv_city;
	// tv_area;// 显示市区和小区
	// RelativeLayout re_choose_xiaoqu;// 小区

	List<String> list = new ArrayList<String>();

	PopupWindow mPopWin;
	ChengShiAdapter chengShiAdapter;

	List<QuYuBean> chengShiBean;// 城市

	// 获取市的各个区
	HousePresenter hourse;
	QuYuAdapter quYuAdapter;
	List<QuYuBean.Children> chengShiChildrenbean;

	/** 弹出框在这个布局的下面 */
	RelativeLayout fabu_main;

	private LinearLayout layout;
	/** 区 */
	ListView cheng_listView;
	/** 区域(各个区域下面的分支) */
	ListView qu_listView;
	/** 区域显示部分 */
	FrameLayout chengqu_layout;

	// 获取市的各个区下面的分支
	HousePresenter hourse02;
	QuAdapter quAdapter;
	List<QuBean> quBean;
	/** 小区 */
	List<QuBean.Children> quBeanChildren;

	XiaoQuYuAdapter xiaoQuYuAdapter;

	/** 城市名字 */
	private String chengName;
	/** 区的code */
	private String quCode;

	/** 小区的code */
	private String xiaoquCode;

	private String QuName;

	List<String> listPicture = new ArrayList<String>();

	ArrayList<AvatarDataBeans> resultData;
	private ChengShiPopupWindow chengshiPop;
	private XiaoQuPopupWindow xiaoquPop;
	private WheelView chengshiFather;
	private WheelView chengshiChild;
	private CityChildrenWheelAdapter cityChildAdpter;
	private CityWheelAdapter nume01;
	private XiaoQuFatherWheelAdapter xiaoquFatherAdapter;
	private XiaoQuChildWheelAdapter xiaoquChildAdapter;
	private WheelView xiaoquFather;
	private WheelView xiaoquChild;
	private String uid;
	protected StringBuffer imgBuffer;
	Intent intent;
	public static ActivityEditHouse context;
	Pattern pattern;//
	StringBuffer bufferTemp = new StringBuffer();
	private String title;
	private String phone;
	private String people;

	public static ActivityEditHouse getInstance() {
		return context;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.fabu_house_close:
			finish();
			break;
		case R.id.fabu_house_ok:
			title = fabu_house_title.getText().toString();
			phone = fabu_house_phone.getText().toString();
			people = fabu_house_people.getText().toString();
			if (title.equals("")) {
				ByAlert.alert("请输入标题");
				return;
			}
			if (xiaoquCode == null) {
				ByAlert.alert("请选择小区");
				return;
			}
			if (phone.equals("")) {
				ByAlert.alert("请输入联系电话");
				return;
			}
//			if (!isMobileNO(phone)) {
//				ByAlert.alert("请输入正确的手机号");
//				return;
//			}
			if (people.equals("")) {
				ByAlert.alert("请输入联系人");
				return;
			}
			imgBuffer = new StringBuffer();
			bufferTemp = new StringBuffer();
			// {"code":-1,"data":"该类型图片需要比例4:3","notify_id":1478567658}
			uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
			List<File> files = new ArrayList<>();
			for (int i = 0; i < listImg.size(); i++) {
				Matcher m = pattern.matcher(listImg.get(i));
				if (!m.matches()) {
					files.add(new File(listImg.get(i)));
				} else {
					bufferTemp.append(listImg.get(i) + ",");
				}
				// String Str = Bimp.drr.get(i).substring(
				// Bimp.drr.get(i).lastIndexOf("/") + 1,
				// Bimp.drr.get(i).lastIndexOf("."));
				// list.add(FileUtils.SDPATH + Str + ".JPEG");
				// files.add(new File(FileUtils.SDPATH + Str + ".JPEG"));
			}
			if (files.size() <= 0 && listImg.size() <= 0) {
				ByAlert.alert("请上传相关房源图片");
				return;
			}
			if (files.size() <= 0) {// 没有上传新图片
				intent = new Intent(ActivityEditHouse.this,
						ActivityFaBuHouseTwo.class);
				// Bundle bundle = new Bundle();
				intent.putExtra("title", title);
				intent.putExtra("imgIds", bufferTemp.toString());
				intent.putExtra("xiaoquCode", xiaoquCode);
				intent.putExtra("phone", phone);
				intent.putExtra("people", people);
				intent.putExtra("house_no", house_no);
				intent.putExtra("address", SPUtils.get(ActivityEditHouse.this, null, SPContants.SELECT_CITY, "")+""+SPUtils.get(ActivityEditHouse.this, null, SPContants.SELECT_AREA, "")+""+xiaoQuArea);
				intent.putExtra(
						"detail",
						SPUtils.get(ActivityEditHouse.this, null,
								SPContants.SELECT_CITY, "") + xiaoQuArea);

				intent.putExtra("model", editBean);
				// intent.putExtras(bundle);house_no
				startActivity(intent);
				return;
			}

			Map<String, String> params = new HashMap<String, String>();
			params.put("uid", uid);
			params.put("type", "house");

			MultipartRequest multipartRequest = new MultipartRequest(
					NetPublicConstant.URL
							+ "/file/upload?client_id=by565fa4facdb191",
					new Listener<String>() {

						@Override
						public void onResponse(String arg0) {
							// TODO Auto-generated method stub
							String code = "";
							String result = "";
							try {
								JSONObject jsonObject = new JSONObject(arg0);
								code = jsonObject.getString("code");
								result = jsonObject.getString("data");
								if (code.equals("-1")) {
									ByAlert.alert(result);
								} else {
									Gson gson = new Gson();
									Type type = new TypeToken<AvatarBeans>() {
									}.getType();
									Log.v("msg", result + "img_ok");
									AvatarBeans avatarBeans = gson.fromJson(
											arg0, type);
									imgBuffer.append(listImg);
									for (AvatarDataBeans bean : avatarBeans
											.getData()) {
										imgBuffer.append(bean.getId() + ",");
									}

									intent = new Intent(ActivityEditHouse.this,
											ActivityFaBuHouseTwo.class);
									// Bundle bundle = new Bundle();
									intent.putExtra("title", title);
									intent.putExtra("imgIds",
											imgBuffer.toString());
									intent.putExtra("xiaoquCode", xiaoquCode);
									intent.putExtra("phone", phone);
									intent.putExtra("people", people);
									intent.putExtra("house_no", house_no);
									intent.putExtra(
											"detail",
											SPUtils.get(ActivityEditHouse.this,
													null,
													SPContants.SELECT_CITY, "")
													+ xiaoQuArea);
									intent.putExtra("address", SPUtils.get(ActivityEditHouse.this, null, SPContants.SELECT_CITY, "")+""+SPUtils.get(ActivityEditHouse.this, null, SPContants.SELECT_AREA, "")+""+xiaoQuArea);
									intent.putExtra("model", editBean);
									// intent.putExtras(bundle);
									startActivity(intent);
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}, new ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError arg0) {
							// TODO Auto-generated method stub
							ByAlert.alert(arg0);
						}
					}, "image[]", files, params);
			MyApplcation.addRequest(multipartRequest);
			if (imgBuffer.toString().length() <= 0) {
				// ByAlert.alert("请先");
				return;
			}
			break;
		case R.id.re_choose_city:// 选择市区
			intent = new Intent(this, ChooseAreaActivity.class);
			startActivityForResult(intent, 101);
			// chengShi();
			// chengshiPop.showAtLocation(v, Gravity.BOTTOM
			// | Gravity.CENTER_HORIZONTAL, 0, 0);
			// chengquWindow(v);

			break;
		// case R.id.re_choose_xiaoqu:// 小区
		// if (quBean != null) {
		// xiaoqu();
		// xiaoquPop.showAtLocation(v, Gravity.BOTTOM
		// | Gravity.CENTER_HORIZONTAL, 0, 0);
		// } else {
		// ByAlert.alert("请先选择城市地区");
		// }
		// // xiqoquWindow(v);
		// break;
		case R.id.huxing_cancel:
			chengshiPop.dismiss();
			break;
		case R.id.huxing_ok:
			QuYuBean quYuBean = null;
			Children children = null;
			try {
				quYuBean = nume01.getPosition(chengshiFather.getCurrentItem());
				children = cityChildAdpter.getChildPoisition(chengshiChild
						.getCurrentItem());
			} catch (Exception e) {
				// TODO: handle exception
			}

			quCode = (children == null ? quYuBean.getCode() : children
					.getCode());
			hourse02 = new HousePresenter(ActivityEditHouse.this);
			hourse02.getQuyuLieBiao02(quCode);

			tv_city.setText(quYuBean.getName() + "  " + children == null ? ""
					: children.getName());
			chengshiPop.dismiss();
			break;
		default:
			break;
		}
	}

	public void guanbi() {
		finish();
	}

	/**
	 * 弹出框
	 */
	private void chengShi() {
		// 户型
		chengshiPop = new ChengShiPopupWindow(this, this, "市区选择");
		chengshiFather = (WheelView) chengshiPop.getContentView().findViewById(
				R.id.huxing_01);
		chengshiFather.setBackgroundResource(R.drawable.white);
		System.out.println(chengShiBean.size() + "城市条目");
		nume01 = new CityWheelAdapter(MyApplcation.ctx, 1, chengShiBean.size());
		nume01.setLabel("室");
		nume01.setDatas(chengShiBean);
		// NumericWheelAdapter nume02 = new
		// NumericWheelAdapter(MyApplcation.ctx,
		// 1, chengShiBean.size());
		chengshiFather.setViewAdapter(nume01);
		chengshiFather.setCyclic(false);// 是否可循环滑动
		chengshiFather.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				setCityChildenData(wheel.getCurrentItem());
				// wheel.getCurrentItem();
			}
		});

		chengshiChild = (WheelView) chengshiPop.getContentView().findViewById(
				R.id.huxing_02);
		chengshiChild.setBackgroundResource(R.drawable.white);
		setCityChildenData(0);
	}

	/**
	 * 弹出框
	 */
	private void xiaoqu() {
		// 选择小区
		xiaoquPop = new XiaoQuPopupWindow(this, xiaoquClickLisenter, "小区选择");
		xiaoquFather = (WheelView) xiaoquPop.getContentView().findViewById(
				R.id.huxing_01);
		xiaoquFather.setBackgroundResource(R.drawable.white);
		System.out.println(quBean.size() + "小区父类条目");
		xiaoquFatherAdapter = new XiaoQuFatherWheelAdapter(MyApplcation.ctx, 1,
				quBean.size());
		xiaoquFatherAdapter.setDatas(quBean);
		xiaoquFather.setViewAdapter(xiaoquFatherAdapter);
		// xiaoquFather.setVisibleItems(3);
		xiaoquFather.setCyclic(false);// 是否可循环滑动
		xiaoquFather.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				setXiaoQuChildenData(wheel.getCurrentItem());
				// wheel.getCurrentItem();
			}
		});

		xiaoquChild = (WheelView) xiaoquPop.getContentView().findViewById(
				R.id.huxing_02);
		xiaoquChild.setBackgroundResource(R.drawable.white);
		setXiaoQuChildenData(0);
	}

	OnClickListener xiaoquClickLisenter = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.huxing_cancel:
				break;
			case R.id.huxing_ok:
				QuBean xiaoquFatherBean = xiaoquFatherAdapter
						.getPosition(xiaoquFather.getCurrentItem());
				com.itboye.ihomebank.adapter.QuBean.Children children = null;
				try {
					children = xiaoquChildAdapter.getPosition(xiaoquChild
							.getCurrentItem());
				} catch (Exception e) {
					// TODO: handle exception
				}
				// tv_area.setText(xiaoquFatherBean.getName() + "  "
				// + (children == null ? "" : children.getName()));
				xiaoquCode = (children == null ? xiaoquFatherBean.getCode()
						: children.getCode());
				break;
			default:
				break;
			}

			xiaoquPop.dismiss();
		}
	};
	private String house_no;
	private HousePresenter housePresenter;

	void setCityChildenData(int index) {
		if (chengShiBean != null) {
			if (chengShiBean.size() > 0) {
				if (chengShiBean.get(index).getChildren().size() > 0) {
					cityChildAdpter = new CityChildrenWheelAdapter(
							MyApplcation.ctx, 1, chengShiBean.get(index)
									.getChildren().size());
					// nume03.setLabel("厅");
					cityChildAdpter.setDatas(chengShiBean.get(index)
							.getChildren());
					chengshiChild.setViewAdapter(cityChildAdpter);
					chengshiChild.setCyclic(false);// 是否可循环滑动
					chengshiChild
							.addScrollingListener(new OnWheelScrollListener() {

								@Override
								public void onScrollingStarted(WheelView wheel) {
									// TODO Auto-generated method stub

								}

								@Override
								public void onScrollingFinished(WheelView wheel) {
									// TODO Auto-generated method stub

								}
							});
				}
			}
		}
	}

	void setXiaoQuChildenData(int index) {
		if (quBean != null) {
			if (quBean.size() > 0) {
				if (quBean.get(index).getChildren().size() > 0) {
					xiaoquChildAdapter = new XiaoQuChildWheelAdapter(
							MyApplcation.ctx, 1, quBean.get(index)
									.getChildren().size());
					// nume03.setLabel("厅");
					xiaoquChildAdapter
							.setDatas(quBean.get(index).getChildren());
					xiaoquChild.setViewAdapter(xiaoquChildAdapter);
					xiaoquChild.setCyclic(false);// 是否可循环滑动
					xiaoquChild
							.addScrollingListener(new OnWheelScrollListener() {

								@Override
								public void onScrollingStarted(WheelView wheel) {
									// TODO Auto-generated method stub

								}

								@Override
								public void onScrollingFinished(WheelView wheel) {
									// TODO Auto-generated method stub

								}
							});
				} else {
					xiaoquChildAdapter = new XiaoQuChildWheelAdapter(
							MyApplcation.ctx, 0, 0);
					xiaoquChildAdapter.notifyDataSetChanged();
					xiaoquChild.setViewAdapter(xiaoquChildAdapter);
				}
			} else {
				xiaoquChildAdapter = new XiaoQuChildWheelAdapter(
						MyApplcation.ctx, 0, 0);
				xiaoquChildAdapter.notifyDataSetChanged();
				xiaoquChild.setViewAdapter(xiaoquChildAdapter);
			}
		} else {
			xiaoquChildAdapter.notifyDataSetChanged();
			xiaoquChild.setViewAdapter(xiaoquChildAdapter);
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_fabu_house;
	}

	@Override
	protected void onInitController() {
		pattern = Pattern.compile("^[0-9]*$");
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		house_no = getIntent().getStringExtra("house_no");
		if (house_no == null) {// 发布房源

		} else {// 修改房源
			housePresenter = new HousePresenter(this);
			housePresenter.houseEdit(uid, house_no);
		}
		fabu_house_phone.setEnabled(false);
		fabu_house_phone.setText(SPUtils.get(this, null, SPContants.MOBILE, "")
				+ "");
		String sex = "";
		if ((SPUtils.get(this, null, SPContants.SEX, "") + "").equals("0")) {// 保密
			int length = (SPUtils.get(this, null, SPContants.RealName, "") + "")
					.length();
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < length - 1; i++) {
				sex += "*";
			}

		} else if ((SPUtils.get(this, null, SPContants.SEX, "") + "")
				.equals("1")) {//
			sex = "女士";
		} else if ((SPUtils.get(this, null, SPContants.SEX, "") + "")
				.equals("2")) {
			sex = "先生";
		}
		fabu_house_people.setEnabled(false);
		fabu_house_people.setText((SPUtils.get(this, null, SPContants.RealName,
				"") + "").charAt(0) + sex);
		noScrollgridview = (XGridView) findViewById(R.id.noScrollgridview);
		noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));

		noScrollgridview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == listImg.size()) {
					new PopupWindows(ActivityEditHouse.this, noScrollgridview);
				} else {
					Intent intent = new Intent(ActivityEditHouse.this,
							CopyOfPhotoActivity.class);
					intent.putExtra("ID", arg2);
					intent.putExtra("type", 0);
					startActivityForResult(intent, 101);
				}
			}
		});

	}

	TextView fabu_house_v;

	@Override
	protected void onInit() {
		context = this;
		hourse = new HousePresenter(this);
		hourse.getQuyuLieBiao("");
		fabu_house_v.setVisibility(View.GONE);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	private static final int TAKE_PICTURE_FANG_YUAN = 001;
	private String path = "";
	private String xiaoQuArea;
	private EditBean editBean;
	public static List<String> listImg = new ArrayList<>();

	public void photo() {
		Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory()
				+ "/myimage/", String.valueOf(System.currentTimeMillis())
				+ ".jpg");
		path = file.getPath();
		Uri imageUri = Uri.fromFile(file);
		openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		startActivityForResult(openCameraIntent, TAKE_PICTURE_FANG_YUAN);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case TAKE_PICTURE_FANG_YUAN:
			if (Bimp.drr.size() < 9 && resultCode == -1) {
				// Bimp.drr.add(path);
				listImg.add(path);
				adapter.notifyDataSetChanged();
			}
			break;

		}
		if (requestCode == 101 && resultCode == 102) {
			fabu_house_img.setImageResource(R.drawable.wutu);
		}
		if (requestCode == 101 && resultCode == 103) {
			// fabu_house_img.setImageBitmap(Bimp.bmp.get(Bimp.bmp.size() - 1));
		}
		if (requestCode == 101 && resultCode == 104) {
			if (data != null) {
				xiaoquCode = data.getStringExtra("xiaoqu");
				xiaoQuArea = data.getStringExtra("xiaoquName");
				tv_city.setText(xiaoQuArea);
			}
		}
	}

	protected void onRestart() {
		adapter = new GridAdapter(this);
		noScrollgridview.setAdapter(adapter);
		if(listImg.size()>0)
		XImageLoader.load("file://"+listImg.get(listImg.size()-1), fabu_house_img);
		else
			fabu_house_img.setImageResource(R.drawable.wutu);
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		listImg.clear();
		// Bimp.bmp.remove(TAKE_PICTURE);
	}

	public class PopupWindows extends PopupWindow {

		public PopupWindows(Context mContext, View parent) {

			View view = View
					.inflate(mContext, R.layout.item_popupwindows, null);
			view.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.fade_ins));
			LinearLayout ll_popup = (LinearLayout) view
					.findViewById(R.id.ll_popup);
			ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.push_bottom_in_2));

			setWidth(LayoutParams.FILL_PARENT);
			setHeight(LayoutParams.FILL_PARENT);
			setBackgroundDrawable(new BitmapDrawable());
			setFocusable(true);
			setOutsideTouchable(true);
			setContentView(view);
			showAtLocation(parent, Gravity.BOTTOM, 0, 0);
			update();

			Button bt1 = (Button) view
					.findViewById(R.id.item_popupwindows_camera);
			Button bt2 = (Button) view
					.findViewById(R.id.item_popupwindows_Photo);
			Button bt3 = (Button) view
					.findViewById(R.id.item_popupwindows_cancel);
			bt1.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					photo();
					dismiss();
				}
			});
			bt2.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent(ActivityEditHouse.this,
							TestPicActivity.class);
					intent.putExtra("max", 9);
					startActivity(intent);
					dismiss();
				}
			});
			bt3.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dismiss();
				}
			});

		}
	}

	
	public class GridAdapter extends BaseAdapter {
		private LayoutInflater inflater; // 视图容器
		private int selectedPosition = -1;// 选中的位置
		private boolean shape;

		public boolean isShape() {
			return shape;
		}

		public void setShape(boolean shape) {
			this.shape = shape;
		}

		public GridAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return (listImg.size() + 1);
		}

		public Object getItem(int arg0) {

			return null;
		}

		public long getItemId(int arg0) {

			return 0;
		}

		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}

		public int getSelectedPosition() {
			return selectedPosition;
		}

		/**
		 * ListView Item设置
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
			final int coord = position;
			ViewHolder holder = null;
			if (convertView == null) {

				convertView = inflater.inflate(R.layout.item_published_grida,
						parent, false);
				holder = new ViewHolder();
				holder.image = (ImageView) convertView
						.findViewById(R.id.item_grida_image);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (position == listImg.size()) {
				holder.image.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.icon_addpic_unfocused));
				// if (listImg.size() > 0){
				// try {
				// Integer.parseInt(listImg.get(position-1));
				// XImageLoader.load(NetPublicConstant.IMAGE_URL+listImg.get(position-1),
				// holder.image);
				// } catch (Exception e) {
				// // TODO: handle exception
				// XImageLoader.load("file://"+listImg.get(position-1),
				// holder.image);
				// }
				//
				// // fabu_house_img.setImageBitmap(Bimp.bmp.get(position - 1));
				// }
				if (position == 9) {
					holder.image.setVisibility(View.GONE);
				}
			} else {
				try {
					Integer.parseInt(listImg.get(position));
					XImageLoader
							.load(NetPublicConstant.IMAGE_URL
									+ listImg.get(position), holder.image);
				} catch (Exception e) {
					// TODO: handle exception
					XImageLoader.load("file://" + listImg.get(position),
							holder.image);
				}
				// holder.image.setImageBitmap(listImg.get(position));
			}

			return convertView;
		}

		public class ViewHolder {
			public ImageView image;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.quyuLieBiao_success) {
				chengShiBean = (List<QuYuBean>) entity.getData();

				chengShiChildrenbean = new ArrayList<QuYuBean.Children>();

				for (int i = 0; i < chengShiBean.size(); i++) {
					chengShiChildrenbean.addAll(chengShiBean.get(i)
							.getChildren());// 得到市下面的区
				}
			}
			if (entity.getEventType() == HousePresenter.quyuLieBiao_fail) {
				ByAlert.alert(chengShiChildrenbean.toString());
			}

			if (entity.getEventType() == HousePresenter.quyuLieBiao02_success) {
				quBean = (List<QuBean>) entity.getData();

				quBeanChildren = new ArrayList<QuBean.Children>();

				for (int i = 0; i < quBean.size(); i++) {
					quBeanChildren.addAll(quBean.get(i).getChildren());// 得到区域下面的小区
				}
			}

			if (entity.getEventType() == HousePresenter.quyuLieBiao02_fail) {
				ByAlert.alert(entity.getData());
			}
			if (entity.getEventType() == HousePresenter.houseEdit_success) {
				editBean = (EditBean) entity.getData();
				listImg = editBean.getShowImgs();
				adapter = new GridAdapter(this);
				noScrollgridview.setAdapter(adapter);
				if(listImg.size()>0){
					XImageLoader.load(NetPublicConstant.IMAGE_URL+listImg.get(listImg.size()-1), fabu_house_img);
				}
				fabu_house_title.setText(editBean.getTitle());
				xiaoquCode = editBean.getCommunity();
				title = editBean.getTitle();
				tv_city.setText(editBean.getCommunityName());

				fabu_house_phone.setText(editBean.getContactPhone());

				fabu_house_people.setText(editBean.getContactName());
			}
			if (entity.getEventType() == HousePresenter.houseEdit_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

	/** 判断是否为手机号 */
	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	@Override
	public void OnSuccess(ArrayList<AvatarDataBeans> resultData) {

		AvatarDataBeans bean = new AvatarDataBeans();
		for (int i = 0; i < resultData.size(); i++) {
			bean.setId(resultData.get(i).getId());

			listPicture.add(resultData.get(i).getId());
		}
	}

	@Override
	public void OnError(String resultData) {
		ByAlert.alert("上传失败");

	}

}
